package com.hnevc.hellochartsdemo;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.ComboLineColumnChartData;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.util.ChartUtils;
import lecho.lib.hellocharts.view.ColumnChartView;

public class MainActivity extends AppCompatActivity {

    private ColumnChartData data;
    private ColumnChartView chart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<BasicPoorpeopleModel> list = new ArrayList<>();
        BasicPoorpeopleModel basicPoorpeopleModel = new BasicPoorpeopleModel();

        basicPoorpeopleModel.setCulturelevelid("1");
        basicPoorpeopleModel.setWhcd_pc(40);
        list.add(basicPoorpeopleModel);
        basicPoorpeopleModel.setCulturelevelid("2");
        basicPoorpeopleModel.setWhcd_pc(60);
        list.add(basicPoorpeopleModel);
        basicPoorpeopleModel.setCulturelevelid("3");
        basicPoorpeopleModel.setWhcd_pc(80);
        list.add(basicPoorpeopleModel);

        chart = (ColumnChartView) findViewById(R.id.column_chart);
        generateData(list);


    }


    private void generateData(List<BasicPoorpeopleModel> list){
        //每个集合显示几条柱子
        int numSubcolumns = 1;
        //显示多少个集合
        int numColumns = list.size();
        //保存所有的柱子
        List<Column> columns = new ArrayList<Column>();
        //保存每个竹子的值
        List<SubcolumnValue> values;
        List<AxisValue> axisXValues = new ArrayList<AxisValue>();
        //对每个集合的柱子进行遍历
        for (int i = 0; i < numColumns; ++i) {

            values = new ArrayList<SubcolumnValue>();
            //循环所有柱子（list）
            for (int j = 0; j < numSubcolumns; ++j) {
                //创建一个柱子，然后设置值和颜色，并添加到list中
                values.add(new SubcolumnValue(list.get(i).getWhcd_pc(), ChartUtils.pickColor()));
                //设置X轴的柱子所对应的属性名称
                axisXValues.add(new AxisValue(i).setLabel(list.get(i).getCulturelevelid()));
            }
            //将每个属性的拥有的柱子，添加到Column中
            Column column = new Column(values);
            //是否显示每个柱子的Lable
            column.setHasLabels(true);
            //设置每个柱子的Lable是否选中，为false，表示不用选中，一直显示在柱子上
            column.setHasLabelsOnlyForSelected(false);
            //将每个属性得列全部添加到List中
            columns.add(column);
        }
        //设置Columns添加到Data中
        data = new ColumnChartData(columns);
        //设置X轴显示在底部，并且显示每个属性的Lable，字体颜色为黑色，X轴的名字为“学历”，每个柱子的Lable斜着显示，距离X轴的距离为8
        data.setAxisXBottom(new Axis(axisXValues).setHasLines(true).setTextColor(Color.BLACK).setName("学历").setHasTiltedLabels(true).setMaxLabelChars(8));
        //属性值含义同X轴
        data.setAxisYLeft(new Axis().setHasLines(true).setName("人数").setTextColor(Color.BLACK).setMaxLabelChars(5));
        //最后将所有值显示在View中
        chart.setColumnChartData(data);

    }
}
